%12 AUgust 2005
% demo of reading sh coeff and computing fields
% tested the B_r component with Pomme2.5 (Pomme 16/17 deg and CO2 29 deg)
% the internal B_r component very much matches

fname = 'c:\manoj\geomag\mf\co2.cof';
%fname = 'c:\manoj\champ\mf4\mf4.cof';
[m_i, epoch, maxdeg_MF, maxdeg_SV, r_ref, m_ext] = read_model(fname);
%Nils code needs only three lines as header
%% obtain ALL main field coefficients
koeff_MF_total_N = maxdeg_MF*(maxdeg_MF+2);
m_MF = m_i(1:koeff_MF_total_N);
%% obtain ALL secular coefficients
m_SV = m_i(koeff_MF_total_N+1:end);
%% obtain Dst coefficients
m_ext_Dst = m_ext(9:11);
%% obtain external coefficients
m_ext_0 = zeros(8,5);
m_ext_0(:,1) = m_ext(1:8);
m_ext_0(1,2:5) = m_ext(12:15)';
m_ext_0(4,2:5) = m_ext(16:19)';  

%r_ref = r_ref + 430; %altitude
%------------------------

nval = 1;
for i = 0.5:359.5,
    
    lat = 1:180;
    long = ones([1,length(lat)])*i;
    r = ones([1,length(lat)])*r_ref + 0;
    theta = lat;
    phi = long;
    [A_r, A_theta, A_phi] = design_SHA(r'/r_ref, theta'*(pi/180), phi'*(pi/180), 29, 'int');
    B_r(:,nval) = A_r*m_MF;
    nval = nval +1;
end;